package utils

import (
	"fmt"
	"math"
	"strconv"
	"strings"
)

func HsbToRgb(h, s, b float64) (r, g, bi int) {
	i := int(math.Abs(h/60)) % 6
	fmt.Println("i =", i)

	f := h/60 - float64(i)
	p := (b * 255) * (1 - s)
	q := (b * 255) * (1 - (f * s))
	t := (b * 255) * (1 - ((1 - f) * s))

	var (
		R float64
		G float64
		B float64
	)

	switch i {
	case 0:
		R = q
		G = t
		B = p
	case 1:
		R = q
		G = t
		B = p
	case 2:
		R = p
		G = q
		B = t
	case 3:
		R = p
		G = q
		B = t
	case 4:
		R = t
		G = p
		B = q
	case 5:
		R = t
		G = p
		B = q
	}
	return round(R), round(G), round(B)
}
func round(x float64) int {
	return int(math.Floor(x + 0.5))
}

type rgb struct {
	red, green, blue int64
}

func NewRgb(r, g, b int64) *rgb {
	return &rgb{
		r, g, b,
	}
}

type HEX struct {
	str string
}

func (h *HEX) ReturnHexInt() string {
	return "0x" + strings.ToUpper(h.str)
}
func t2x(t int64) string {
	result := strconv.FormatInt(t, 16)
	if len(result) == 1 {
		result = "0" + result
	}
	return result
}

func (color *rgb) Rgb2hex() *HEX {
	r := t2x(color.red)
	g := t2x(color.green)
	b := t2x(color.blue)
	return &HEX{r + g + b}
}

func (color HEX) hex2rgb() rgb {
	r, _ := strconv.ParseInt(color.str[:2], 16, 10)
	g, _ := strconv.ParseInt(color.str[2:4], 16, 18)
	b, _ := strconv.ParseInt(color.str[4:], 16, 10)
	return rgb{r, g, b}
}
